$(function() {
	$('#sel_age_min').hover(function() {
		setAge("#sel_age_min", 18);
		$('#sel_age_min_items li').click(function() {
			$('#sel_age_min span').html($(this).html());
			if ($(this).html() > $('#sel_age_max span').html()) {
				$('#sel_age_max span').html('不要求');
			}
			$('#sel_age_min ol').hide();
		});
	}, function() {
		$('#sel_age_min ol').hide();
	});
	$('#sel_age_max').hover(function() {
		var age = 18;
		if ($('#sel_age_min span').html() != "不要求") {
			age = parseInt($('#sel_age_min span').html());
		}
		setAge("#sel_age_max", age);
		$('#sel_age_max_items li').click(function() {
			$('#sel_age_max span').html($(this).html());
			$('#sel_age_max ol').hide();
		});
	}, function() {
		$('#sel_age_max ol').hide();
	});
	$('#sel_province').hover(function() {
		setProvince();
		$('#sel_province span').click(function() {
			$('#sel_province span').removeClass('current');
			$(this).addClass('current');
			$('#sel_province span').first().html($(this).html());
			$('#sel_city span').html('全省');
			$('#sel_province ul').hide();
		});
	}, function() {
		$('#sel_province ul').hide();
	});

	$('#sel_city').hover(function() {
		setCity();
		$('#sel_city li').click(function() {
			$('#sel_city li').removeClass('current');
			$(this).addClass('current');
			$('#sel_city span').html($(this).html());
			$('#sel_city ol').hide();
		});
	}, function() {
		$('#sel_city ol').hide();
	});

	$('#sel_sign').hover(function() {
		$('#sel_sign ol').show();
		$('#sel_sign_items li').click(function() {
			$('#sel_sign_items li').removeClass('current');
			$(this).addClass('current');
			$('#sel_sign span').html($(this).html());
			$('#sel_sign_items').hide();
		});
	}, function() {
		$('#sel_sign ol').hide();
	});

	$('#xz_slideBtn').click(function() {
		if ($('#xz_slideBtn font').html() == '收起') {
			$('#xzDescDiv').slideUp();
			$('#xz_slideBtn font').html('展开');
		} else {
			$('#xzDescDiv').slideDown();
			$('#xz_slideBtn font').html('收起');
		}
		return false;
	});

	$('#searchBtn').click(function() {
		submitForm(1);
		return false;
	});

	$('.sayHiBtnP').click(function() {
		var userid = $(this).attr('href');
		$.post("mailBoxAjax_sayHello", {
			"message.receiverUser" : userid
		}, function(data) {
			if (data != "true") {
				art.dialog.alert('发生未知错误！！！').time(2);
			} else {
				art.dialog.alert('打招呼成功').time(2);
			}
		});
		return false;
	});
	setPageNum();
	$('.renqun_type a').click(function() {
		$('.renqun_type a').removeClass('current');
		$(this).addClass('current');
	});
});
/*
 * 设置年龄
 */
function setAge(ageId, minAge) {
	var ageOl = $(ageId).find('ol');
	ageOl.empty();
	var defAge = parseInt($(ageId).find('span').html());
	ageOl.append('<li class="popup_item">不要求</li>');
	for (var i = minAge; i <= 80; i += 1) {
		var s = "";
		if (defAge == i) {
			s = '<li class="popup_item current">' + i + '岁</li>';
		} else {
			s = '<li class="popup_item">' + i + '岁</li>';
		}
		ageOl.append(s);
	}
	ageOl.show();
}

/*
 * 设置页数
 */
function setPageNum() {
	var pageNum = parseInt($('#pageNum').html());
	var currentPage = parseInt($('#searchForm input:eq(4)').val());
	var aStr = "";
	for (var i = 1; i <= pageNum; i++) {
		if (currentPage == i) {
			aStr += '<a class="current" href="#">' + i + '</a>';
		} else {
			aStr += '<a href="#">' + i + '</a>';
		};
	}
	$('.pe_left').after(aStr);
	$('.page a').click(function() {
		var page = parseInt($(this).html());
		if (page >= 1) {
			if (page != currentPage) {
				submitForm(page);
			}
		}
		return false;
	});
	$('.pe_left').click(function() {
		if (currentPage <= 1) {
			art.dialog.alert('已经到第一页了').time(2);
		} else {
			submitForm(currentPage - 1);
		}
		return false;
	});
	$('.pe_right').click(function() {
		if (currentPage >= pageNum) {
			art.dialog.alert('已经是最后一页了').time(2);
		} else {
			submitForm(currentPage + 1);
		}
		return false;
	});

}

/*
 * 设置省份
 */
function setProvince() {
	$('#sel_province_items li').empty();
	var provinces = ["北京市", "天津市", "上海市", "重庆市", "河北省", "山西省", "内蒙古", "辽宁省", "吉林省", "黑龙江", "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西", "海南省", "四川省", "贵州省", "云南省", "西藏", "陕西省", "甘肃省", "青海省", "宁夏", "新疆", "香港", "澳门", "台湾省"];
	var defAge = $('#sel_province').find('span').first().html();
	for (var i = 0; i < provinces.length; i++) {
		var s = "";
		if (defAge == provinces[i]) {
			s = '<span class="popup_item current">' + provinces[i] + '</span>';
		} else {
			s = '<span class="popup_item">' + provinces[i] + '</span>';
		}
		if (i < 12) {
			$('#sel_province_items li').eq(0).append(s);
		} else if (i < 24) {
			$('#sel_province_items li').eq(1).append(s);
		} else {
			$('#sel_province_items li').eq(2).append(s);
		}
	}
	$('#sel_province_items').show();
}

/*
 * 设置城市
 */
function setCity() {
	var provinces = ["北京市", "天津市", "上海市", "重庆市", "河北省", "山西省", "内蒙古", "辽宁省", "吉林省", "黑龙江", "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西", "海南省", "四川省", "贵州省", "云南省", "西藏", "陕西省", "甘肃省", "青海省", "宁夏", "新疆", "香港", "澳门", "台湾省"];
	var cityOl = $('#sel_city ol');
	cityOl.empty();
	cityOl.append('<li class="popup_item">全省</li>');
	var province = $('#sel_province').find('span').first().html();
	var defCity = $('#sel_city').find('span').first().html();
	for (var i = 0; i < provinces.length; i++) {
		if (province == provinces[i]) {
			var citys = dsy.Items['0_' + i];
			for (var i = 0; i < citys.length; i++) {
				var s = "";
				if (defCity == citys[i]) {
					s = '<li class="popup_item current">' + citys[i] + '</li>';
				} else {
					s = '<li class="popup_item">' + citys[i] + '</li>';
				}
				cityOl.append(s);
			}
		}
	}
	cityOl.show();
}

/*
 * 提交表单
 */

function submitForm(pageNum) {
	// 取得要提交的参数
	var formVal = new Array();
	formVal[0] = $('#sel_age_min span').html();
	formVal[1] = $('#sel_age_max span').html();
	formVal[2] = $('#sel_province span').first().html();
	formVal[3] = $('#sel_city span').first().html();
	formVal[4] = pageNum;
	$('#searchForm input').each(function(index) {
		$(this).attr('value', formVal[index]);
	});
	// 提交表单
	$('#searchForm').submit();
}

